package com.hxzy.controller;

import com.hxzy.common.domain.AjaxResult;
import com.hxzy.common.utils.StringUtils;
import com.hxzy.entity.Area;
import com.hxzy.framework.controller.BaseController;
import com.hxzy.service.AreaService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Stack;

/**
 * 类说明
 *
 * @author admin
 * @date 2021-09-16
 */
@Controller
@RequestMapping(value = "/admin")
public class AreaController extends BaseController {

    @Autowired
    private AreaService areaService;


    /**
     * 显示页面
     * @param parentId
     * @return
     */
    @GetMapping(value = "/area/list/{parentId}")
    public String showPage(@PathVariable(value = "parentId") Long parentId, HttpServletRequest request,Model model){

        String url=generatedUrl(parentId,request);

        model.addAttribute("title",url);
        model.addAttribute("parentId",parentId);
        //一定是第一级
        if(parentId.equals(0L)){

            //是否允许点击显示下级列表
            model.addAttribute("open",1);
         }else {
            Area area = this.areaService.selectByPrimaryKey(parentId);

            //是否允许点击显示下级列表
            if(area.getLevel().equals(1)){
                model.addAttribute("open",1);
            }else{
                model.addAttribute("open",0);
            }
        }

        //查询它所有的数据
        List<Area> areaList = this.areaService.findByParentId(parentId);
        model.addAttribute("areaList",areaList);
        return "system/area/list";
    }

    /**
     * 生成url地址
     * @param parentId
     * @return
     */
    private String generatedUrl(Long parentId, HttpServletRequest request) {
        Stack<Long> stack=new Stack<>();
        stack.add(parentId);

        //入栈
        while(!parentId.equals(0L)){
            Area area=this.areaService.selectByPrimaryKey(parentId);
            parentId=area.getParentId();
            stack.add(parentId);
        }

        String projectContext=request.getContextPath();
        StringBuffer str=new StringBuffer();
        //出栈
        Long id=null;
        while ( stack.size()>0 &&  (id=stack.pop())!=null){
            //查询名称
            if(id.equals(0L)){
                String url=StringUtils.format("<a href='{}/admin/area/list/{}'>{}</a>",projectContext,id,"全国");
                str.append(url);
            }else{
                Area areaObj=this.areaService.selectByPrimaryKey(id);
                String url=StringUtils.format("<a href='{}/admin/area/list/{}'>{}</a>",projectContext,id,areaObj.getAreaName());
                str.append(url);
            }
            if(stack.size()>0){
                str.append(" > ");
            }

        }
        return str.toString();
    }


    @GetMapping(value = "/area/add/{parentId}")
    public String addPage(@PathVariable(value = "parentId") Long parentId,Model model){
        model.addAttribute("parentId",parentId);
        if(parentId.equals(0L)){
            model.addAttribute("parentName","根目录");
            model.addAttribute("level",1);
        }else{
            Area area = this.areaService.selectByPrimaryKey(parentId);
            model.addAttribute("parentName",area.getAreaName());
            model.addAttribute("level",area.getLevel()+1);
        }
        return "system/area/add";
    }

    @ResponseBody
    @PostMapping(value = "/area/add")
    public AjaxResult addData(Area area){
        int count = this.areaService.insert(area);
        return super.toAjax(count);
    }


    @GetMapping(value = "/area/edit/{areaId}")
    public String editPage(@PathVariable(value = "areaId") Long areaId,Model model){
        Area area = this.areaService.selectByPrimaryKey(areaId);
        if(area.getParentId().equals(0L)){
            model.addAttribute("parentName","根目录");
        }else{
            Area parentArea=this.areaService.selectByPrimaryKey(area.getParentId());
            model.addAttribute("parentName",parentArea.getAreaName());
        }

        model.addAttribute("area",area);
        return "system/area/edit";
    }

    @ResponseBody
    @PostMapping(value = "/area/edit")
    public AjaxResult editData(Area area){
        int count = this.areaService.updateByPrimaryKeySelective(area);
        return super.toAjax(count);
    }
}
